import { getColorTextLoadMap } from '@/utils';

/** 处理数据字典 */
const contractStateData = (loadList: Record<string, any>) => {
  let loadInfo: Record<string, any> = {};
  loadList?.forEach((item: any) => {
    const { colorText } = getColorTextLoadMap(item?.name);
    loadInfo[colorText] = {
      id: item?.id,
      name: colorText,
    };
  });
  return loadInfo;
};

/** 处理合同状态汇总管理 */
const handleAllStateData = (info: Record<string, any>, loadInfo: Record<string, any>) => {
  // 分别获取收款付款的数据
  let putAwayKeys: string[] = []; //收款时间
  let putAwayData: Record<string, any>[] = []; //收款数据

  let paymentKeys: string[] = []; // 付款时间
  let paymentData: Record<string, any>[] = []; // 付款数据

  let putTimeData: Record<string, any> = {}; //收款时间数据
  let payTimeData: Record<string, any> = {}; //付款时间数据

  Object?.keys(info)?.forEach((itemType: any) => {
    //遍历接口中收付款数据
    const itemTypeInfo: Record<string, any> = info[itemType];

    if (itemType == 0) {
      // 付款
      payTimeData = itemTypeInfo;
    } else if (itemType == 1) {
      //收款
      putTimeData = itemTypeInfo;
    }

    Object?.keys(itemTypeInfo)?.forEach((itemTypeKey: any) => {
      //遍历接口中按照月份的数据
      const itemTypeList: Record<string, any>[] = itemTypeInfo[itemTypeKey] ? itemTypeInfo[itemTypeKey] : [];

      if (itemType == 0) {
        paymentKeys?.push(itemTypeKey);
      } else if (itemType == 1) {
        putAwayKeys?.push(itemTypeKey);
      }

      itemTypeList?.forEach((itemTypeValue: any) => {
        if (itemType == 0) {
          // 付款
          paymentData?.push(itemTypeValue);
        } else if (itemType == 1) {
          //收款
          putAwayData?.push(itemTypeValue);
        }
      });
    });
  });

  let topStateNameList: string[] = []; //状态名称集合
  let topStatePayNums: number[] = []; // 上方状态付款数量
  let topStatePaySums: number[] = []; //上方状态付款金额
  let topStatePutNums: number[] = []; //上方收款数量
  let topStatePutSums: number[] = []; //上方状态收款金额
  let stateNameNumSums: string[] = []; //合同状态数量金额
  let payStateConfig: Record<string, any>[] = []; //付款状态数量金额配置
  let putStateConfig: Record<string, any>[] = []; //收款状态数量金额配置

  Object?.keys(loadInfo)?.forEach((itemState: any) => {
    //遍历数据字典
    topStateNameList?.push(loadInfo[itemState]?.name);

    stateNameNumSums?.push(`${loadInfo[itemState]?.name}数量`);
    stateNameNumSums?.push(`${loadInfo[itemState]?.name}金额`);

    let a_topStatePayNums: number = 0; //付款数量
    let a_topStatePaySums: number = 0; //付款金额
    let b_topStatePutNums: number = 0; //收款数量
    let b_topStatePutSums: number = 0; //收款金额

    paymentData?.forEach((itemPay: any) => {
      if (loadInfo[itemState]?.id == itemPay?.state) {
        a_topStatePayNums += itemPay?.totalNum;
        a_topStatePaySums += itemPay?.totalSum;
      }
    });

    topStatePayNums?.push(a_topStatePayNums);
    topStatePaySums?.push(a_topStatePaySums);

    putAwayData?.forEach((itemPut: any) => {
      if (loadInfo[itemState]?.id == itemPut?.state) {
        b_topStatePutNums += itemPut?.totalNum;
        b_topStatePutSums += itemPut?.totalSum;
      }
    });
    topStatePutNums?.push(b_topStatePutNums);
    topStatePutSums?.push(b_topStatePutSums);

    let payNumList: number[] = []; //按照时间付款数量集合
    let paySumList: number[] = []; //按照时间付款金额集合

    let putNumList: number[] = []; //按照时间收款数量集合
    let putSumList: number[] = []; //按照时间收款数量集合

    Object?.keys(payTimeData)?.forEach((itemPayTime: any) => {
      let statePayNums: number = 0; //付款数量
      let statePaySums: number = 0; //付款金额

      payTimeData?.[itemPayTime]?.forEach((itemPayChild: any) => {
        if (itemPayChild?.state == loadInfo?.[itemState]?.id) {
          statePayNums += itemPayChild?.totalNum;
          statePaySums += itemPayChild?.totalSum;
        }
      });
      payNumList?.push(statePayNums);
      paySumList?.push(statePaySums);

      let statePutNums: number = 0; //收款数量
      let statePutSums: number = 0; //收款金额
      putTimeData?.[itemPayTime]?.forEach((itemPutChild: any) => {
        if (itemPutChild?.state == loadInfo?.[itemState]?.id) {
          statePutNums += itemPutChild?.totalNum;
          statePutSums += itemPutChild?.totalSum;
        }
      });
      putNumList?.push(statePutNums);
      putSumList?.push(statePutSums);
    });

    payStateConfig?.push({
      //保存付款数量
      name: loadInfo[itemState]?.name + '数量',
      type: 'bar',
      data: payNumList,
      stack: '数量',
      label: { show: true, position: 'inside' },
    });
    payStateConfig?.push({
      //保存付款金额
      name: loadInfo[itemState]?.name + '金额',
      type: 'bar',
      data: paySumList,
      stack: '金额',
      label: { show: true, position: 'inside' },
    });

    putStateConfig?.push({
      //保存收款数量
      name: loadInfo[itemState]?.name + '数量',
      type: 'bar',
      data: putNumList,
      stack: '数量',
      label: { show: true, position: 'inside' },
    });
    putStateConfig?.push({
      //保存收款金额
      name: loadInfo[itemState]?.name + '金额',
      type: 'bar',
      data: putSumList,
      stack: '金额',
      label: { show: true, position: 'inside' },
    });
  });

  return {
    topStateNameList,
    topStatePayNums,
    topStatePaySums,
    topStatePutNums,
    topStatePutSums,
    putAwayKeys,
    paymentKeys,
    stateNameNumSums,
    putAwayData,
    paymentData,
    payStateConfig,
    putStateConfig,
  };
};

/** 处理金额从大到小，且取前10位 */
const sumSortDecs = (infoData: Record<string, any>[]) => {
  const sortList: Record<string, any>[] = infoData?.sort((a, b) => {
    return b?.totalSum - a?.totalSum
  })
  return sortList?.splice(0, 9)
}

/** 处理合同相对方数据 */
const handleOppositeData = (info: Record<string, any>) => {

  let oppositeNameList: string[] = [];  //相对方名称集合

  let payPutList: Record<string, any>[] = []//收款付款集合
  let payDataList: Record<string, any>[] = []  //付款集合
  let putDataList: Record<string, any>[] = []  //收款集合

  Object?.keys(info)?.forEach((itemType: any) => {
    info[itemType]?.forEach((itemInfo: any) => {
      payPutList?.push(itemInfo)  //合并首付款数据，并排序处理

      if (itemInfo?.capitalDir == 0) {
        //付款
        payDataList?.push(itemInfo)
      } else {
        //收款
        putDataList?.push(itemInfo)
      }
    });
  });

  /** 将数据排序，并获取前十位金额数据 */
  const sortTenList: Record<string, any>[] = sumSortDecs(payPutList)

  let payNumNameList: Record<string, any>[] = []  //相对方付款数量
  let paySumNameList: Record<string, any>[] = []  //相对方付款金额
  let putNumNameList: Record<string, any>[] = []  //相对方收款数量
  let putSumNameList: Record<string, any>[] = []  //相对方收款金额

  sortTenList?.forEach((itemSort: any) => {
    oppositeNameList?.push(itemSort?.oppositeName)

    payNumNameList?.push({
      name: itemSort?.oppositeName,
      value: itemSort?.capitalDir == 0 ? itemSort?.totalNum : 0,
    })
    paySumNameList?.push({
      name: itemSort?.oppositeName,
      value: itemSort?.capitalDir == 0 ? itemSort?.totalSum : 0,
    })

    putNumNameList?.push({
      name: itemSort?.oppositeName,
      value: itemSort?.capitalDir == 1 ? itemSort?.totalNum : 0,
    })
    putSumNameList?.push({
      name: itemSort?.oppositeName,
      value: itemSort?.capitalDir == 1 ? itemSort?.totalSum : 0,
    })
  })

  let paySearchNames: string[] = [] //搜索收款相对方名称
  let paySearchNum: Record<string, any>[] = [] //搜索收款相对方数量
  let paySearchSum: Record<string, any>[] = [] //搜索收款相对方金额

  let putSearchNames: string[] = []//搜索付款相对方名称
  let putSearchNum: Record<string, any>[] = [] //搜索付款相对方数量
  let putSearchSum: Record<string, any>[] = [] //搜索付款相对方金额

  payDataList?.forEach((itemPay: any) => {
    paySearchNames?.push(itemPay?.oppositeName)
    paySearchNum?.push({
      name: itemPay?.oppositeName,
      value: itemPay?.totalNum ? itemPay?.totalNum : 0
    })
    paySearchSum?.push({
      name: itemPay?.oppositeName,
      value: itemPay?.totalSum ? itemPay?.totalSum : 0
    })
  })

  putDataList?.forEach((itemPut: any) => {
    putSearchNames?.push(itemPut?.oppositeName)
    putSearchNum?.push({
      name: itemPut?.oppositeName,
      value: itemPut?.totalNum ? itemPut?.totalNum : 0
    })
    putSearchSum?.push({
      name: itemPut?.oppositeName,
      value: itemPut?.totalSum ? itemPut?.totalSum : 0
    })
  })

  return {
    oppositeNameList,
    payNumNameList,
    paySumNameList,
    putNumNameList,
    putSumNameList,

    paySearchNames,
    paySearchNum,
    paySearchSum,
    putSearchNames,
    putSearchNum,
    putSearchSum,
  };
};

export { contractStateData, handleAllStateData, handleOppositeData };
